﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Alarm_interface
{
    [Serializable]
    public class info_editing_Bas : DataGridView
    {

		private ContextMenuStrip CMenuStrip;

		private int MROW = -1;

		public info_editing_Bas()
		{
			SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
			UpdateStyles();

			BindMenu();
			ContextMenuStrip = CMenuStrip;
			//优化dataGridView控件闪耀现象
			Type dgvType = base.GetType();
			PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
			pi.SetValue(this, true, null);

			//禁止编辑后自动生成一行
			base.AllowUserToAddRows = false;
			base.BackgroundColor = Color.White;
			base.BorderStyle = BorderStyle.None;
			base.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			base.EnableHeadersVisualStyles = false;
			base.GridColor = SystemColors.ActiveCaption;
			base.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			base.RowTemplate.Height = 23;
			base.ScrollBars = ScrollBars.Both;
			base.DefaultCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			base.RowHeadersWidth = 60;
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			dataGridViewCellStyle.BackColor = Color.FromArgb(245, 245, 255);
			dataGridViewCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			dataGridViewCellStyle.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle.SelectionBackColor = SystemColors.Highlight;
			dataGridViewCellStyle.SelectionForeColor = SystemColors.HighlightText;
			dataGridViewCellStyle.WrapMode = DataGridViewTriState.True;
			base.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
			dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle2.BackColor = Color.FromArgb(245, 245, 255);
			dataGridViewCellStyle2.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			dataGridViewCellStyle2.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight;
			dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText;
			dataGridViewCellStyle2.WrapMode = DataGridViewTriState.True;
			base.RowHeadersDefaultCellStyle = dataGridViewCellStyle2;

			//禁止用户改变列宽行高
			base.AllowUserToOrderColumns = false;
			base.AllowUserToResizeRows = false;
			base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			base.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

			//禁止排序
			//if (base.Columns.Count > 0)
			//{
			//	for (int i = 0; i < base.Columns.Count; i++)
			//	{
			//		base.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
			//	}
			//}
		}

		protected override CreateParams CreateParams
		{
			get
			{
				CreateParams cp = base.CreateParams;
				cp.ExStyle |= 0x02000000;
				return cp;
			}
		}

		private void BindMenu()
		{
			CMenuStrip = new ContextMenuStrip();

			ToolStripItem toolStripItem5 = new ToolStripMenuItem();
			toolStripItem5.Name = "添加ToolStripMenuItem";
			toolStripItem5.Text = "添加行 ";
			toolStripItem5.Click += 添加ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem5);

			ToolStripItem toolStripItem = new ToolStripMenuItem();
			toolStripItem.Name = "复制ToolStripMenuItem";
			toolStripItem.Text = "复制";
			toolStripItem.Click += 复制ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem);
			ToolStripItem toolStripItem2 = new ToolStripMenuItem();
			toolStripItem2.Name = "粘贴ToolStripMenuItem";
			toolStripItem2.Text = "粘贴";
			toolStripItem2.Click += 粘贴ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem2);
			ToolStripItem toolStripItem3 = new ToolStripMenuItem();
			toolStripItem3.Name = "删除ToolStripMenuItem";
			toolStripItem3.Text = "删除 (将需要删除的行，整行选中才可以)";
			toolStripItem3.Click += 删除ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem3);
			ToolStripItem toolStripItem4 = new ToolStripMenuItem();
			//toolStripItem4.Name = "插入ToolStripMenuItem";
			//toolStripItem4.Text = "插入行";
			//toolStripItem4.Click += 插入ToolStripMenuItem_Click;
			//CMenuStrip.Items.Add(toolStripItem4);

			ToolStripItem toolStripItem6 = new ToolStripMenuItem();
			toolStripItem6.Name = "清除ToolStripMenuItem";
			toolStripItem6.Text = "清除 (清除选中的内容)";
			toolStripItem6.Click += 清除ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem6);
		}

		protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
		{
			try
			{
				SizeF sizeF = e.Graphics.MeasureString("1", base.DefaultCellStyle.Font);
				SolidBrush brush = new SolidBrush(base.RowHeadersDefaultCellStyle.ForeColor);
				e.Graphics.DrawString((e.RowIndex + 1).ToString(CultureInfo.CurrentUICulture).PadLeft(4, '0'), base.DefaultCellStyle.Font, brush, e.RowBounds.Location.X + 12, (float)e.RowBounds.Location.Y + ((float)e.RowBounds.Height - sizeF.Height) / 2f);
			}
			catch (Exception)
			{
			}
			base.OnRowPostPaint(e);
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			MROW = GetRowIndexAt(e.Y);
			base.OnMouseDown(e);
		}

		protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex > -1 && e.ColumnIndex > -1 && e.Button == MouseButtons.Right && !base.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
			{
				base.CurrentCell = base.Rows[e.RowIndex].Cells[e.ColumnIndex];
				base.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
			}
			base.OnCellMouseDown(e);
		}

		private List<int[]> DALL()
		{
			List<int[]> list = new List<int[]>();
			int cellCount = GetCellCount(DataGridViewElementStates.Selected);
			if (cellCount > 0)
			{
				AreAllCellsSelected(includeInvisibleCells: true);
				new StringBuilder();
				for (int i = 0; i < cellCount; i++)
				{
					list.Add(new int[2]
					{
						base.SelectedCells[i].RowIndex,
						base.SelectedCells[i].ColumnIndex
					});
				}
			}
			return list;
		}

		public int GetRowIndexAt(int mouseLocation_Y)
		{
			if (base.FirstDisplayedScrollingRowIndex < 0)
			{
				return -1;
			}
			if (base.ColumnHeadersVisible && mouseLocation_Y <= base.ColumnHeadersHeight)
			{
				return -1;
			}
			int num = base.FirstDisplayedScrollingRowIndex;
			int num2 = DisplayedRowCount(includePartialRow: true);
			int num3 = 1;
			while (num3 <= num2)
			{
				if (base.Rows[num].Visible)
				{
					Rectangle rowDisplayRectangle = GetRowDisplayRectangle(num, cutOverflow: true);
					if (rowDisplayRectangle.Top <= mouseLocation_Y && mouseLocation_Y < rowDisplayRectangle.Bottom)
					{
						return num;
					}
					num3++;
				}
				num++;
			}
			return -1;
		}

		protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
		{
			base.OnCellEndEdit(e);
			;
		}

		private void GVCellEndEdit(int R, int C)
		{
		}

		private void GVCellBeginEdit(int R, int C)
		{
		}

		protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
		{
			base.OnCellBeginEdit(e);
			GVCellBeginEdit(e.RowIndex, e.ColumnIndex);
		}

		private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				for (int num = base.SelectedRows.Count - 1; num > -1; num--)
				{
					base.Rows.Remove(base.SelectedRows[num]);
				}
			}
			catch (Exception)
			{
			}
		}

		private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				Clipboard.SetDataObject(GetClipboardContent());
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "ERR");
			}
		}

		private void 插入ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				for (int i = 0; i < 1; i++)
				{
					base.Rows.Insert(MROW);
				}
			}
			catch (Exception)
			{
			}
		}

		private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				for (int i = 0; i < 1; i++)
				{
					DataGridViewRowCollection rows = base.Rows;
					object[] values = new string[base.ColumnCount];
					rows.Add(values);
				}
			}
			catch (Exception)
			{
			}
		}

		private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string text = Clipboard.GetText();
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			int num = 0;
			int num2 = 0;
			for (int i = 0; i < text.Length; i++)
			{
				if (text.Substring(i, 1) == "\n")
				{
					num++;
				}
				if (text.Substring(i, 1) == "\t")
				{
					num2++;
				}
			}
			if (text.Substring(text.Length - 1, 1) == "\n")
			{
				num--;
			}
			num2 /= num + 1;
			object[,] array = new object[num + 1, num2 + 1];
			string text2 = null;
			for (int j = 0; j < num + 1; j++)
			{
				for (int k = 0; k < num2 + 1; k++)
				{
					text2 = null;
					if (k == num2 && text.IndexOf("\r") != -1)
					{
						text2 = text.Substring(0, text.IndexOf("\r"));
					}
					if (k == num2 && text.IndexOf("\r") == -1)
					{
						text2 = text.Substring(0);
					}
					if (k != num2)
					{
						text2 = text.Substring(0, text.IndexOf("\t"));
						text = text.Substring(text.IndexOf("\t") + 1);
					}
					if (text2 == string.Empty)
					{
						text2 = null;
					}
					array[j, k] = text2;
				}
				text = text.Substring(text.IndexOf("\n") + 1);
			}
			int num3 = -1;
			int num4 = -1;
			int num5 = -1;
			int num6 = -1;
			if (base.SelectedCells.Count != 0)
			{
				num5 = base.SelectedCells[0].ColumnIndex;
				num6 = base.SelectedCells[0].RowIndex;
			}
			foreach (DataGridViewCell selectedCell in base.SelectedCells)
			{
				num3 = selectedCell.ColumnIndex;
				if (num3 > num5)
				{
					num3 = num5;
				}
				else
				{
					num5 = num3;
				}
				num4 = selectedCell.RowIndex;
				if (num4 > num6)
				{
					num4 = num6;
				}
				else
				{
					num6 = num4;
				}
				selectedCell.Selected = false;
			}
			for (int l = 0; l < num + 1; l++)
			{
				for (int m = 0; m < num2 + 1; m++)
				{
					int num7 = l + num4;
					int num8 = m + num3;
					if (num7 >= base.RowCount || num8 >= base.ColumnCount)
					{
						continue;
					}
					try
					{
						if (base.Columns[num8].GetType().ToString() == "System.Windows.Forms.DataGridViewComboBoxColumn")
						{
							int count = ((DataGridViewComboBoxColumn)base.Columns[num8]).Items.Count;
							for (int n = 0; n < count; n++)
							{
								if (((DataGridViewComboBoxColumn)base.Columns[num8]).Items[n].ToString() == array[l, m].ToString())
								{
									GVCellBeginEdit(num7, num8);
									base.Rows[num7].Cells[num8].Value = array[l, m];
									base.Rows[num7].Cells[num8].Selected = true;
									GVCellEndEdit(num7, num8);
									break;
								}
							}
						}
						else
						{
                            //GVCellBeginEdit(num7, num8);
                            base.Rows[num7].Cells[num8].Value = array[0, m+1];
                            base.Rows[num7].Cells[num8].Selected = true;
                            //GVCellEndEdit(num7, num8);
                        }
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				for (int i = 0; i < base.SelectedCells.Count; i++)
				{
					if (base.Columns[base.SelectedCells[i].ColumnIndex].GetType().ToString() != "System.Windows.Forms.DataGridViewComboBoxColumn")
					{
						base.SelectedCells[i].Value = "";
					}
				}
			}
			catch (Exception)
			{
			}
		}


	}
}
